﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager : BaseManager
{

	private Transform _canvasTransform;

	private Dictionary<PanelType, GameObject> _uiPanelPrefabDict;

	private Dictionary<PanelType, BasePanel> _instantiatedPanelDict;

	private Stack<BasePanel> _panelStack;

	public Dictionary<PanelType, BasePanel> InstantiatedPanelDict
	{
		get { return _instantiatedPanelDict; }
	}

	/// <summary>
	/// 加载面板
	/// </summary>
	/// <param name="panelType"></param>
	/// <returns></returns>
	public BasePanel PushPanel(PanelType panelType)
	{
		BasePanel panel = null;
		_instantiatedPanelDict.TryGetValue(panelType, out panel);
		if (panel == null)
		{
			//说明该面板从未实例化过（从未入栈），此处需要实例化
			GameObject panelPrefab = _uiPanelPrefabDict[panelType];
			GameObject panelGO = GameObject.Instantiate(panelPrefab, _canvasTransform, false);
			panel = panelGO.GetComponent<BasePanel>();
			_instantiatedPanelDict.Add(panelType,panel);
		}
		else
		{
			//说明该面板已经实例化，入栈后被出栈，处于被禁用状态，需要重新启用
			panel.gameObject.SetActive(true);
		}
		
		//执行该Panel的OnEnter
		panel.OnEnter();
		if (_panelStack.Count > 0)
		{
			BasePanel peekPanel = _panelStack.Peek();
			peekPanel.OnPause();
		}
		_panelStack.Push(panel);
		return panel;
	}
	/// <summary>
	/// 关闭（弹出）顶层面板
	/// </summary>
	public void PopPanel()
	{
		if (_panelStack.Count > 0)
		{
			BasePanel peekPanel = _panelStack.Pop();
			peekPanel.OnExit();
			if (_panelStack.Count > 0)
			{
				BasePanel secPeekPanel = _panelStack.Peek();
				secPeekPanel.OnResume();
			}
		}
	}
	
	public override void OnInit()
	{
		//获取挂载Canvas的Transform属性
		_canvasTransform = GameObject.Find("Canvas").transform;
		//初始化面板预制体字典
		_uiPanelPrefabDict = new Dictionary<PanelType, GameObject>();
		_uiPanelPrefabDict.Add(PanelType.Main,Resources.Load<GameObject>("UIPanel/Panel_Main"));
		_uiPanelPrefabDict.Add(PanelType.Register,Resources.Load<GameObject>("UIPanel/Panel_Register"));
		_uiPanelPrefabDict.Add(PanelType.Login,Resources.Load<GameObject>("UIPanel/Panel_Login"));
		_uiPanelPrefabDict.Add(PanelType.MessagePanel,Resources.Load<GameObject>("UIPanel/Panel_Message"));
		_uiPanelPrefabDict.Add(PanelType.TestConnectionPanel,Resources.Load<GameObject>("UIPanel/Panel_TestConnection"));
		_uiPanelPrefabDict.Add(PanelType.ChatListPanel,Resources.Load<GameObject>("UIPanel/Panel_ChatList"));
		_uiPanelPrefabDict.Add(PanelType.FriendListPanel,Resources.Load<GameObject>("UIPanel/Panel_FriendList"));
		_uiPanelPrefabDict.Add(PanelType.MyPanel,Resources.Load<GameObject>("UIPanel/Panel_My"));
		_uiPanelPrefabDict.Add(PanelType.SearchFriendPanel,Resources.Load<GameObject>("UIPanel/Panel_SearchFriend"));
		_uiPanelPrefabDict.Add(PanelType.ReplyAddFriendPanel,Resources.Load<GameObject>("UIPanel/Panel_ReplyAddFriend"));
		_uiPanelPrefabDict.Add(PanelType.Chat,Resources.Load<GameObject>("UIPanel/Panel_Chat"));
		_uiPanelPrefabDict.Add(PanelType.Msg,Resources.Load<GameObject>("UIPanel/Panel_Msg"));
		
		//初始化
		_instantiatedPanelDict = new Dictionary<PanelType, BasePanel>();
		_panelStack = new Stack<BasePanel>();
		//加载第一个Panel
		PushPanel(PanelType.Main);
	}

	public override void OnUpdate()
	{
		
	}

	public override void OnDestroy()
	{
		
	}
}

public enum PanelType
{
	Main,
	Register,
	Login,
	MessagePanel,
	TestConnectionPanel,
	ChatListPanel,
	FriendListPanel,
	MyPanel,
	SearchFriendPanel,
	ReplyAddFriendPanel,
	Chat,
	Msg
	
}